package com.ruoyi.quality.service.impl;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.inspectionItem.InspectionItem;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.quality.mapper.InspectionItemMapper;
import com.ruoyi.quality.service.IInspectionItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 检测检验项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-17
 */
@Service
public class InspectionItemServiceImpl implements IInspectionItemService
{
    @Autowired(required = false)
    private InspectionItemMapper inspectionItemMapper;

    /**
     * 查询检测检验项目
     * 
     * @param id 检测检验项目主键
     * @return 检测检验项目
     */
    @Override
    public InspectionItem selectInspectionItemById(String id)
    {
        return inspectionItemMapper.selectInspectionItemById(id);
    }

    /**
     * 查询检测检验项目
     *
     * @return 检测检验项目
     */
    @Override
    public List<InspectionItem> selectInspectionItemByReport(String reportNum, String equType) {
        return inspectionItemMapper.selectInspectionItemByReport(reportNum, equType);
    }

    @Override
    public List<InspectionItem>  selectInspectionItemByPid(String pid) {
        return inspectionItemMapper.selectInspectionItemByPid(pid);
    }

    /**
     * 查询检测检验项目列表
     * 
     * @param inspectionItem 检测检验项目
     * @return 检测检验项目
     */
    @Override
    public List<InspectionItem> selectInspectionItemList(InspectionItem inspectionItem)
    {
        return inspectionItemMapper.selectInspectionItemList(inspectionItem);
    }


    /**
     * 构建树形前端结构
     * @param inspectionItem 检测检验项目
     * @return
     */
    @Override
    public List<InspectionItem> buildInspectionItemTree(List<InspectionItem> inspectionItem) {
        List<InspectionItem> returnList = new ArrayList<InspectionItem>();
        for (Iterator<InspectionItem> iterator = inspectionItem.iterator(); iterator.hasNext();)
        {
            InspectionItem t = (InspectionItem) iterator.next();
            // 根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (StringUtils.equalsIgnoreCase("0", t.getPid()))
            {
                recursionFn(inspectionItem, t);
                returnList.add(t);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = inspectionItem;
        }
        return returnList;
    }


    /**
     *
     * @param inspectionItem 检测检验项目树形结构
     * @return
     */
    @Override
    public List<TreeSelect> buildInspectionItemTreeSelect(List<InspectionItem> inspectionItem) {
        List<InspectionItem> inspectionItemTrees = buildInspectionItemTree(inspectionItem);
        return inspectionItemTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<InspectionItem> list, InspectionItem t)
    {
        // 得到子节点列表
        List<InspectionItem> childList = getChildList(list, t);
        t.setList(childList);
        for (InspectionItem tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                // 判断是否有子节点
                Iterator<InspectionItem> it = childList.iterator();
                while (it.hasNext())
                {
                    InspectionItem n = (InspectionItem) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    /**
     * 新增检测检验项目
     * 
     * @param inspectionItem 检测检验项目
     * @return 结果
     */
    @Override
    public int insertInspectionItem(InspectionItem inspectionItem)
    {
        inspectionItem.setId(IdUtils.fastSimpleUUID());
        inspectionItem.setCreateTime(DateUtils.getNowDate());
        //如果是父级项目pid = 0
        if(StringUtils.isEmpty(inspectionItem.getPid())) {
            inspectionItem.setPid("0");
        }
        return inspectionItemMapper.insertInspectionItem(inspectionItem);
    }

    /**
     * 修改检测检验项目
     * 
     * @param inspectionItem 检测检验项目
     * @return 结果
     */
    @Override
    public int updateInspectionItem(InspectionItem inspectionItem)
    {
        //获取当前id下有没有父级节点，存在则不修改
        List<InspectionItem>  child = selectInspectionItemByPid(inspectionItem.getId());
        if(child.size() > 0) {
            return 0;
        }
        inspectionItem.setUpdateTime(DateUtils.getNowDate());
        return inspectionItemMapper.updateInspectionItem(inspectionItem);
    }

    /**
     * 批量删除检测检验项目
     * 
     * @param ids 需要删除的检测检验项目主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteInspectionItemByIds(String[] ids)
    {
        for(int i = 0 ; i < ids.length; i++) {
            //获取当前id下有没有父级节点，存在则不删除
            List<InspectionItem> inspectionItem = selectInspectionItemByPid(ids[0]);
            if(inspectionItem.size() > 0) {
                return 0;
            }
        }
        return inspectionItemMapper.deleteInspectionItemByIds(ids);
    }

    /**
     * 删除检测检验项目信息
     * 
     * @param id 检测检验项目主键
     * @return 结果
     */
    @Override
    public int deleteInspectionItemById(String id)
    {
        //获取当前id下有没有父级节点，存在则不删除
        List<InspectionItem> inspectionItem = selectInspectionItemByPid(id);
        if(inspectionItem.size() > 0) {
            return 0;
        }
        return inspectionItemMapper.deleteInspectionItemById(id);
    }


    /**
     * 根据Pid删除检测项
     * @param pid
     * @return
     */
    @Override
    public int deleteInspectionItemByPid(String pid) {
        return inspectionItemMapper.deleteInspectionItemByPid(pid);
    }

    /**
     * 得到子节点列表
     */
    private List<InspectionItem> getChildList(List<InspectionItem> list, InspectionItem t)
    {
        List<InspectionItem> tlist = new ArrayList<InspectionItem>();
        Iterator<InspectionItem> it = list.iterator();
        while (it.hasNext())
        {
            InspectionItem n = (InspectionItem) it.next();
            if (StringUtils.equalsIgnoreCase(n.getPid(), t.getId()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<InspectionItem> list, InspectionItem t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
